package com.gbb.common.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

/**
 * @author gaobingbing
 */
public abstract class AbstractPagerAdapter<T> extends PagerAdapter {

	private final Context mContext;

	private final List<T> dataList;

	private final LayoutInflater mInflater;

	public AbstractPagerAdapter(Context mContext,List<T> dataList) {
		this.mContext = mContext;
		this.dataList = dataList;
		mInflater = LayoutInflater.from(this.mContext);
	}

	@Override
	public int getCount() {
		return dataList == null ? 0 : dataList.size();
	}

	@Override
	public boolean isViewFromObject(@NonNull View view,@NonNull Object object) {
		return view == object;
	}

	public abstract int getLayoutId();

	public abstract void convert(View childView,T dataItem,int position);

	@Override
	public Object instantiateItem(@NonNull ViewGroup container,int position) {
		if(dataList == null) {
			return null;
		}
		View childView = mInflater.inflate(getLayoutId(),null);
		convert(childView,dataList.get(position),position);
		container.addView(childView);
		return childView;
	}

	@Override
	public void destroyItem(ViewGroup container,int position,Object object) {
		container.removeView((View)object);
	}

}
